/** @file    win32/processes.cpp
 *  @author  Alessandro Polo
 *  @version $Id: processes.cpp 2335 2010-04-23 21:55:04Z alex $
 *  @brief   multi-platform implementation of process-related functions.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#include "processes.h"

#include <windows.h>
#include <psapi.h> // Process Status API
#pragma comment( lib, "psapi.lib" )
#include <stdio.h>

using namespace std;


//////////////////////////////////////////////////////////////////////////////
long getCurrentProcessPID() {
	return (long)GetCurrentProcessId();
 }

//////////////////////////////////////////////////////////////////////////////
void setEntry( map<string, string>& fields, const string& key, unsigned long value ) {
	char buffer[64];
	// sprintf(buffer,"0x%08X", value );
	//sprintf(buffer,"%u", value );
#ifdef _OS_MINGW
	sprintf(buffer, "%u", value );
#else
	sprintf_s(buffer, 64, "%u", value );
#endif
	fields[key] = string(buffer);
 }

//////////////////////////////////////////////////////////////////////////////
bool retrieveProcessInfo( long pid, map<string, string>& fields ) {
	HANDLE hProcess;
	PROCESS_MEMORY_COUNTERS pmc;
	hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, (DWORD)pid);
	if ( hProcess == NULL )
		return false;

	if ( GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc)) == FALSE ) {
		CloseHandle(hProcess);
		return false;
	 }

	setEntry( fields, "SysInfo_PageFaultCount", pmc.PageFaultCount );
	setEntry( fields, "SysInfo_PeakWorkingSetSize", pmc.PeakWorkingSetSize );
	setEntry( fields, "SysInfo_WorkingSetSize", pmc.WorkingSetSize );
	setEntry( fields, "SysInfo_QuotaPeakPagedPoolUsage", pmc.QuotaPeakPagedPoolUsage );
	setEntry( fields, "SysInfo_QuotaPeakNonPagedPoolUsage", pmc.QuotaPeakNonPagedPoolUsage );
	setEntry( fields, "SysInfo_PagefileUsage", pmc.PagefileUsage );
	setEntry( fields, "SysInfo_PeakPagefileUsage", pmc.PeakPagefileUsage );

	CloseHandle( hProcess );
	return true;
}



